/*
 * $Id$
 *
 * Copyright ( c ) 2014 XuNuo Corporation. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of XuNuo
 * Corporation ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with the terms
 * of the license agreement you entered into with XuNuo Corporation or a XuNuo
 * authorized reseller (the "License Agreement"). XuNuo may make changes to the
 * Confidential Information from time to time. Such Confidential Information may
 * contain errors.
 *
 * EXCEPT AS EXPLICITLY SET FORTH IN THE LICENSE AGREEMENT, XuNuo DISCLAIMS ALL
 * WARRANTIES, COVENANTS, REPRESENTATIONS, INDEMNITIES, AND GUARANTEES WITH
 * RESPECT TO SOFTWARE AND DOCUMENTATION, WHETHER EXPRESS OR IMPLIED, WRITTEN OR
 * ORAL, STATUTORY OR OTHERWISE INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A
 * PARTICULAR PURPOSE. XuNuo DOES NOT WARRANT THAT END USER'S USE OF THE
 * SOFTWARE WILL BE UNINTERRUPTED, ERROR FREE OR SECURE.
 *
 * XuNuo SHALL NOT BE LIABLE TO END USER, OR ANY OTHER PERSON, CORPORATION OR
 * ENTITY FOR INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL
 * DAMAGES, OR DAMAGES FOR LOSS OF PROFITS, REVENUE, DATA OR USE, WHETHER IN AN
 * ACTION IN CONTRACT, TORT OR OTHERWISE, EVEN IF XuNuo HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES. XuNuo' TOTAL LIABILITY TO END USER SHALL NOT
 * EXCEED THE AMOUNTS PAID FOR THE XuNuo SOFTWARE BY END USER DURING THE PRIOR
 * TWELVE (12) MONTHS FROM THE DATE IN WHICH THE CLAIM AROSE.  BECAUSE SOME
 * STATES OR JURISDICTIONS DO NOT ALLOW LIMITATION OR EXCLUSION OF CONSEQUENTIAL
 * OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO END USER.
 *
 * Copyright version 2.0
 */
package com.th.supcom.pds.deptdispensary.client.widget;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Component;
import com.th.supcom.pds.common.constant.KeysConstants;

/**
 * 护理单元药房新增/编辑窗口事件监听类
 * 
 * @author 杨冲
 */
public class NurseVsDispensaryEditListener
{
    private boolean m_isEdit;
    private DeptVsDispensaryButtons m_deptVsDispensaryButtons;
    private NurseVsDispensaryEditWindow m_window;
    private NurseVsDispensaryAction m_action;

    public void setDeptVsDispensaryButtons (DeptVsDispensaryButtons deptVsDispensaryButtons)
    {

        m_deptVsDispensaryButtons = deptVsDispensaryButtons;
    }

    public void setWindow (NurseVsDispensaryEditWindow window)
    {

        m_window = window;
    }

    public void setAction (NurseVsDispensaryAction action)
    {

        m_action = action;
    }

    public void setEdit (boolean isEdit)
    {

        m_isEdit = isEdit;
    }

    /**
     * 初始化事件监听
     */
    public void initListeners ()
    {
        addCancelListener ();
        addConfirmListener ();
        addContinueListener ();
    }

    /**
     * 新增取消按钮
     */
    private void addCancelListener ()
    {
        m_deptVsDispensaryButtons.getCancelButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_window.hide ();
            }
        });
    }

    /**
     * 新增确认按钮
     */
    private void addConfirmListener ()
    {
        m_deptVsDispensaryButtons.getConfirmButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_action.editDeptVsDispensary (m_isEdit, false);
            }
        });
    }

    /**
     * 新增的时候
     */
    private void addContinueListener ()
    {
        m_deptVsDispensaryButtons.getAddContinueButton ().addSelectionListener (new SelectionListener <ButtonEvent> ()
        {

            @Override
            public void componentSelected (ButtonEvent ce)
            {
                m_action.editDeptVsDispensary (false, true);
            }
        });
    }

    /**
     * 添加快捷键事件
     */
    public void addEnterKeyListener (Component component)
    {
        new KeyNav <ComponentEvent> (component)
        {
            @Override
            public void onKeyPress (ComponentEvent ce)
            {
                int keyCode = ce.getKeyCode ();

                if (!ce.isAltKey ())
                {
                    return;
                }

                executeAltKey (keyCode);
            }
        };
    }

    /**
     * 执行Alt键事件
     */
    private void executeAltKey (int keyCode)
    {
        if (KeysConstants.VK_O == keyCode)
        {
            m_action.editDeptVsDispensary (m_isEdit, false);
        }

        if (KeysConstants.VK_W == keyCode)
        {
            m_action.editDeptVsDispensary (false, true);
        }

        if (KeysConstants.VK_C == keyCode)
        {
            m_window.hide ();
        }
    }
}
